package com.wave.spring.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.*;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import javax.persistence.EntityManager;
import java.io.Serializable;

/**
 * Created by lv on 2017/8/27.
 */
public class CustomFactoryBean<T extends JpaRepository<S,ID>,S,ID extends Serializable> extends JpaRepositoryFactoryBean<T,S,ID> {
     /**
     * Creates a new {@link JpaRepositoryFactoryBean} for the given repository interface.
     *
     * @param repositoryInterface must not be {@literal null}.
     */
    public CustomFactoryBean(Class<? extends T> repositoryInterface) {
        super(repositoryInterface);
    }

    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager){
        return new CustomRepositoryFactory(entityManager);
    }

    private static class CustomRepositoryFactory extends JpaRepositoryFactory {
        public CustomRepositoryFactory(EntityManager entityManager) {
            super(entityManager);
        }


        protected <T, ID extends Serializable> SimpleJpaRepository<T, ID> getTargetRepository(
                RepositoryInformation information, EntityManager entityManager) {

         //   JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());

            return new CustomRepositoryImpl<T,ID>((Class<T>)information.getDomainType(),entityManager);
        }

        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {

      return  CustomRepositoryImpl.class;
        }



    }
}
